package com.hdl.school.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.hdl.school.application.MyApplication;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    /**
     * 获取系统当前时间
     *
     * @param outFormate 输出格式:yy MM dd HH:mm:ss
     * @return 返回当前时间
     */
    public static String getCurTimeToString(String outFormate) {
        if(outFormate==null){
            outFormate="yy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(outFormate);
        Date curDate = new Date(currentStap());// 获取当前时间
        String str = formatter.format(curDate);
        return str;

    }

    public static Date getCurTimeToDate() {
        Date curDate = new Date(currentStap());// 获取当前时间
        return curDate;

    }

    public static Date string2Date(String str, String formate) {
        SimpleDateFormat formatter = new SimpleDateFormat(formate);
        Date date = null;
        try {
            date = formatter.parse(str);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            date = null;
        }
        return date;

    }

    public static String date2String(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dStr = formatter.format(date);
        return dStr;
    }

    public static String date2String(Date date, String formate) {
        if(formate==null){
            formate="yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(formate);
        String dStr = formatter.format(date);
        return dStr;
    }
    /**
     * 日期转字符串
     *
     * @param time       日期时间戳
     * @return
     */
    public static String long2String(long time) {
        return long2String(time,"yyyy-MM-dd HH:mm:ss");
    }


    /**
     * 日期转字符串
     *
     * @param time       日期时间戳
     * @param outFormate
     * @return
     */
    public static String long2String(long time, String outFormate) {
        if(outFormate==null){
            outFormate="yy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(outFormate);
        Date curDate = new Date(time);// 获取当前时间
        String str = formatter.format(curDate);
        return str;
    }


    public static String todayStr(){
       Calendar c = Calendar.getInstance();
        c.setTime(new Date(currentStap()));
        String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
       String  mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if("1".equals(mWay)){
            mWay ="天";
        }else if("2".equals(mWay)){
            mWay ="一";
        }else if("3".equals(mWay)){
            mWay ="二";
        }else if("4".equals(mWay)){
            mWay ="三";
        }else if("5".equals(mWay)){
            mWay ="四";
        }else if("6".equals(mWay)){
            mWay ="五";
        }else if("7".equals(mWay)){
            mWay ="六";
        }
        return mYear + "-" +(Integer.parseInt(mMonth)<10?"0"+mMonth:mMonth)  + "-" + (Integer.parseInt(mDay)<10?"0"+mDay:mDay)+""+"  星期"+mWay;
    }


    public static long currentStap(){
        return System.currentTimeMillis()+MyApplication.timeoffset;
    }


    public static long timeInToday(String time){
        String t=getCurTimeToString("yyyy-MM-dd")+" "+time;
        if(time.length()==5){
            t+=":00";
        }
        long tt=string2Date(t,"yyyy-MM-dd HH:mm:ss").getTime();
        return tt;
    }


    public static void setTimeOffset(Context context, long offset){
        SharedPreferences sharedPreferences=context.getSharedPreferences("school",0);
        sharedPreferences.edit().putLong("timeoffset",offset).commit();
        MyApplication.getAppContext().timeoffset=offset;
    }

}
